package com.web.dao;

import com.web.entity.Message;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * @author FDjavaone
 */
public interface MessageMapper {


    @Delete({
            "delete from t_message",
            " where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into t_message (target, target_name, ",
            " title, content, ",
            " `from`, from_name, create_time, ",
            " modify_time)",
            " values (#{target,jdbcType=BIGINT}, #{targetName,jdbcType=VARCHAR}, ",
            " #{title,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR}, ",
            " #{from,jdbcType=BIGINT}, #{fromName,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, ",
            " #{modifyTime,jdbcType=TIMESTAMP})"
    })
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insert(Message record);

    @InsertProvider(type = MessageSqlProvider.class, method = "insertSelective")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insertSelective(Message record);


    @Select({
            "select",
            " id, target, target_name, title, content, `from`, from_name, create_time, modify_time",
            " from t_message",
            " where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "target", property = "target", jdbcType = JdbcType.BIGINT),
            @Result(column = "target_name", property = "targetName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "title", property = "title", jdbcType = JdbcType.VARCHAR),
            @Result(column = "content", property = "content", jdbcType = JdbcType.VARCHAR),
            @Result(column = "`from`", property = "from", jdbcType = JdbcType.BIGINT),
            @Result(column = "from_name", property = "fromName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    Message selectByPrimaryKey(Long id);


    @UpdateProvider(type = MessageSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Message record);

    @Update({
            "update t_message",
            " set target = #{target,jdbcType=BIGINT},",
            " target_name = #{targetName,jdbcType=VARCHAR},",
            " title = #{title,jdbcType=VARCHAR},",
            " content = #{content,jdbcType=VARCHAR},",
            " `from` = #{from,jdbcType=BIGINT},",
            " from_name = #{fromName,jdbcType=VARCHAR},",
            " create_time = #{createTime,jdbcType=TIMESTAMP},",
            " modify_time = #{modifyTime,jdbcType=TIMESTAMP}",
            " where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Message record);

    @Select({
            "select",
            " id, target, target_name, title, content, `from`, from_name, create_time, modify_time",
            " from t_message",
            " where target = #{id,jdbcType=BIGINT}  order by create_time desc"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "target", property = "target", jdbcType = JdbcType.BIGINT),
            @Result(column = "target_name", property = "targetName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "title", property = "title", jdbcType = JdbcType.VARCHAR),
            @Result(column = "content", property = "content", jdbcType = JdbcType.VARCHAR),
            @Result(column = "`from`", property = "from", jdbcType = JdbcType.BIGINT),
            @Result(column = "from_name", property = "fromName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<Message> selectListByMid(@Param("id") long id);
}